package scnu.dao.impl;

import java.util.List;

import scnu.common.page.Pagination;
import scnu.dao.EnrollmentDao;
import scnu.entity.Enrollment;
import org.springframework.stereotype.Repository;
import scnu.common.persistence.Finder;
import scnu.common.persistence.HibernateBaseDao;

@Repository
public class EnrollmentDaoImpl extends HibernateBaseDao<Enrollment,Integer>
       implements EnrollmentDao {


	@Override
	public Enrollment addEnrollment(Enrollment enrollment) {
		// TODO Auto-generated method stub
		getSession().save(enrollment);
		return enrollment;
	}

	@Override
	public Pagination findEnrollmentByJourneyId(Integer journey_id,int pageNo,int pageSize) {
		// TODO Auto-generated method stub
		Pagination pagination=null;
		System.out.println("from Enrollment bean where bean.journey_id="+journey_id.toString());
		Finder finder=Finder.create("from Enrollment bean where bean.journey_id="+journey_id.toString());
		pagination=find(finder,pageNo,pageSize);
		return pagination;
	}

	@Override
	public Pagination findEnrollmentByUserId(Integer user_id,int pageNo,int pageSize) {
		// TODO Auto-generated method stub
		Pagination pagination=null;
		Finder finder=Finder.create("from Enrollment bean where bean.user_id="+user_id.toString());
		pagination=find(finder,pageNo,pageSize);
		return pagination;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Enrollment> findUserJourneyEnrollment(Integer journey_id,
			Integer user_id) {
		// TODO Auto-generated method stub
		String hql=("from Enrollment bean where bean.journey_id="+journey_id.toString()+"and bean.user_id="+user_id.toString());
		return find(hql);
	}

	@Override
	public int getUserValidEnrollmentNum(Integer user_id) {
		// TODO Auto-generated method stub
		@SuppressWarnings("rawtypes")
		List list=null;
		Finder finder=Finder.create("from Enrollment bean where payment_status=1 and user_id="+user_id.toString());
		list=find(finder);
		return list.size();
	}

	@Override
	public int getJourneyEnrollmentNum(Integer journey_id) {
		// TODO Auto-generated method stub
		@SuppressWarnings("rawtypes")
		List list=null;
		Finder finder=Finder.create("from Enrollment bean where journey_id="+journey_id.toString());
		list=find(finder);
		return list.size();
	}

	@Override
	public int getPayedJourneyEnrollmentNum(Integer journey_id) {
		// TODO Auto-generated method stub
		@SuppressWarnings("rawtypes")
		List list=null;
		Finder finder=Finder.create("from Enrollment bean where journey_id="+journey_id.toString()+"and payment_status=1");
		list=find(finder);
		return list.size();
	}

	@Override
	protected Class<Enrollment> getEntityClass() {
		// TODO Auto-generated method stub
		return Enrollment.class;
	}

	@Override
	public Enrollment updateEnrollment(Enrollment enrollment) {
		// TODO Auto-generated method stub
		getSession().update(enrollment);
		return enrollment;
	}

	@Override
	public Enrollment deleteEnrollmentById(Integer enrollment_id) {
		// TODO Auto-generated method stub
		Enrollment entity=super.get(enrollment_id);
		if(entity!=null)
		{
			getSession().delete(entity);
		}
		return entity;
	}

}
